<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{ 
            background-color: aquamarine;
        }
        canvas{
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <canvas id="tree" width="1000" height="850">当前浏览器不支持canvas</canvas>
    <script>
        const canvas=document.querySelector('canvas')
        const ctx=canvas.getContext('2d')
        ctx.translate(canvas.width/2, canvas.height)
        ctx.scale(1,-1)   //重置坐标系
        drawBranch([0,0],200,30,90)
        function drawBranch(v0, len, thick, angle){
            if(thick<10 && Math.random()<0.3){
                return
            }
            if(thick<2){
                ctx.beginPath()
                ctx.arc(...v0, 10, 0, 2*Math.PI)
                ctx.fillStyle='#fff'
                ctx.fill()
                return
            }
            ctx.beginPath()
            ctx.moveTo(...v0)
            const v1 = [
                v0[0] + len * Math.cos(angle*Math.PI/180),
                v0[1] + len * Math.sin(angle*Math.PI/180)
            ]
            ctx.lineTo(...v1)
            ctx.strokeStyle='#333'
            ctx.lineWidth = thick
            ctx.lineCap = 'round'
            ctx.stroke()

            drawBranch(v1, len*0.8, thick*0.7, angle+Math.random()*30)
            drawBranch(v1, len*0.8, thick*0.7, angle-Math.random()*30)
        }

    </script>
</body>
</html>